package com.socodethis.spaceex.mainmenuscreen.actors;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.socodethis.spaceex.Assets;
import com.socodethis.spaceex.customGame;
import com.socodethis.spaceex.splashscreen.SplashScreen;
import com.socodethis.spaceex.utils.ScreenSize;

public class MenuButton extends Actor {

	private Texture tex;
	public String name;
	public Boolean planetOrbitFront;
	
	public MenuButton(int x, int y, Texture tex){
		this.tex = tex;
		this.x=x;
		this.y=y;
		this.width=ScreenSize.getScaledWidth(tex.getWidth());
		this.height=ScreenSize.getScaledHeight(tex.getHeight());
	}
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		batch.draw(tex, this.x, this.y, this.width, this.height);
	}

	@Override
	public Actor hit(float x, float y) {
		
		return null;
	}
	
	@Override
	public void act(float delta) {
		
	}

	public void update(customGame game){
		if (Gdx.input.justTouched()){
        	Rectangle touchPos = new Rectangle(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY(),1,1);
        	if (touchPos.overlaps(new Rectangle(this.x,this.y,this.width,this.height))){
        		String filename = Assets.manager.getAssetFileName(tex);
        		if (filename.equals("btnPlay.png")){
        			//Play the game
        			game.setScreen(new SplashScreen(game));
        		}
        		else if (filename.equals("btnExit.png")){
        			//Exit the game
        			Gdx.app.exit();
        		}
        	}
        }
		
		if (this.name!=null){
			//Orbit the small planet
			if (this.name.equals("planetSmall")){
				if (this.planetOrbitFront){
					this.x++;
					if (this.x >= (Gdx.graphics.getWidth()/2)+((int)ScreenSize.getScaledWidth(Assets.manager.get("mediumPlanet9.png", Texture.class).getWidth())/2)){
						this.planetOrbitFront=false;
					}
				}
				else{
					this.x--;
					if (this.x <= (Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(Assets.manager.get("mediumPlanet9.png", Texture.class).getWidth())/2)-(int)ScreenSize.getScaledWidth(this.tex.getWidth())){
						this.planetOrbitFront=true;
					}
				}
			}
		}
	}
	
}
